﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using InformationSystem.BLL.Core.Model;

namespace InformationSystem.WebSite.UtilityPage
{
    public partial class DataImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                if (Session["DataImportFileName"] == null)
                    Session.Add("DataImportFileName", string.Empty);
            }

        }


        protected void UploadClick(object sender, DirectEventArgs e)
        {
            // HttpFileCollection coll = Request.Files;
            if (this.fileUpLoad.HasFile)
            {
                //检测文件类型
                if (this.fileUpLoad.PostedFile.ContentType == "application/vnd.ms-excel" && Path.GetExtension(this.fileUpLoad.PostedFile.FileName) == ".xls")
                {
                    //Server.MapPath;
                    string savePath = Path.Combine(Server.MapPath("~"), Common.UploadTemp);

                    string filename = Guid.NewGuid().ToString() + Path.GetExtension(fileUpLoad.PostedFile.FileName);
                    this.fileUpLoad.PostedFile.SaveAs(Path.Combine(savePath, filename));

                    if (Session["DataImportFileName"] != null && Session["DataImportFileName"].ToString() != string.Empty)
                    {
                        if (File.Exists(Path.Combine(savePath, Session["DataImportFileName"].ToString())))
                            File.Delete(Path.Combine(savePath, Session["DataImportFileName"].ToString()));
                    }
                    Session["DataImportFileName"] = filename;
                    btnNext.Disabled = false;
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "信息",
                        Message = "上传成功！"
                    });
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = "错误",
                        Message = "上传的文件类型目前不支持！"
                    });
                    btnNext.Disabled = true;

                }

            }
            else
            {
                btnNext.Disabled = true;
            }

            //e.Success = true;
        }

        protected void Next_Click(object sender, DirectEventArgs e)
        {
            int index = int.Parse(e.ExtraParams["index"]);
            if ((index + 1 < this.WizardPanel.Items.Count))
            {
                if (index + 1 == 2)
                {
                    //判断Dest 表格是否选中，否不让跳转
                    if (DestCmb.SelectedIndex == -1)
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR,
                            Title = "错误",
                            Message = "没有选择目标表格！"
                        });
                        return;
                    }

                }
                this.WizardPanel.ActiveIndex = index + 1;
            }

            if (this.WizardPanel.ActiveIndex == 2)
            {
                LoadExcelField();
            }
            if (this.WizardPanel.ActiveIndex == 1)
            {
                LoadDestTableDate();
            }
            this.CheckButtons();
        }

        protected void Prev_Click(object sender, DirectEventArgs e)
        {
            int index = int.Parse(e.ExtraParams["index"]);
            if ((index - 1) >= 0)
            {
                this.WizardPanel.ActiveIndex = index - 1;
            }
            this.CheckButtons();
        }

        private void CheckButtons()
        {
            int index = this.WizardPanel.ActiveIndex;
            this.btnNext.Disabled = index == this.WizardPanel.Items.Count - 1;
            this.btnPrev.Disabled = index == 0;
        }

        protected void cmbData_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.cbmStore.DataSource = new object[]
            {
                 new object[]{"ID","String","编号"},
                new object[]{"Name1","String","名称"},
                new object[]{"Name2","String","名称"},
                  new object[]{"Name3","String","名称"},
                 new object[]{"Name4","String","名称"}
            };
            this.cbmStore.DataBind();
        }

        protected void btnImport_Click(object sender, DirectEventArgs e)
        {

        }

        protected void DestCmb_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.LoadDestTableDate();
        }
        private void LoadDestTableDate()
        {
            this.DestCmbStore.DataSource = new object[]
            {
                 new object[]{"User","用户",},
                new object[]{"Mark","成绩"},
            };
            this.DestCmbStore.DataBind();
        }

        private void LoadExcelField()
        {
            this.GridStore.DataSource = new object[]
            {
                new object[]{"ID","String","ID"},
                new object[]{"Name","String",""}
            };
            this.GridStore.DataBind();

            this.cbmStore.DataSource = new object[]
            {
                 new object[]{"ID","String","编号"},
                new object[]{"Name1","String","名称"},
                new object[]{"Name2","String","名称"},
                  new object[]{"Name3","String","名称"},
                 new object[]{"Name4","String","名称"}
            };
            this.cbmStore.DataBind();
        }

    }


}